package com.prd.common.baseMapper;

import com.prd.common.constant.Constants;
import com.prd.common.constant.ResultCodeConstants;
import com.prd.common.constant.ResultMessageConstants;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.List;


/**
 * Service rpc层返回结果
 * 
 * @author xqf
 *
 */
public class ServiceResult {
	private static final long serialVersionUID = -6925924956850004727L;

	// 状态编码
	private Integer code = ResultCodeConstants.SUCCESS;

	// 消息
	private String message;

	// 执行结果明细
	private List<ExcuteResult> excutes;
	
 private Object result;

	public ServiceResult() {
	}

	public static ServiceResult success(Object object){
		return success(object,"");
	}
	//此处object值赋给message
	public static ServiceResult success2(String object){
		return success("",object);
	}

	public static ServiceResult success(Object object, String message){
		ServiceResult serviceResult = new ServiceResult();
		serviceResult.setCode(ResultCodeConstants.SUCCESS);
		serviceResult.setValue(object);
		serviceResult.setMessage(message);
		return serviceResult;
	}

	public static ServiceResult warn(String message){
		ServiceResult serviceResult = new ServiceResult();
		serviceResult.setCode(ResultCodeConstants.WARN);
		serviceResult.setMessage(message);
		return serviceResult;
	}

	public static ServiceResult error(String message){
		ServiceResult serviceResult = new ServiceResult();
		serviceResult.setCode(ResultCodeConstants.FAIL);
		serviceResult.setMessage(message);
		return serviceResult;
	}
	
 public Object getValue() {
     return result;
 }

 public void setValue(Object value) {
     this.result = value;
 }

		public ServiceResult(Integer code) {
		this.code = code;
	}

	public Integer getCode() {
		return code;
	}

	public List<ExcuteResult> getExcutes() {
		return excutes;
	}

	public String getMessage() {
		return message;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public void setExcutes(List<ExcuteResult> excutes) {
		this.excutes = excutes;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setMessage(ResultMessageConstants message) {
		this.message = message.name();
	}

	/**
	 * 转换为api层返回结果
	 * 
	 * @param message
	 * @return
	 */
	public ApiResult toApiResult(String message) {
		ApiResult apiResult = toApiResult();

		apiResult.setMessage(message);

		return apiResult;
	}

	/**
	 * 请求是否成功
	 * 
	 * @return
	 */
	public Boolean isSuccess() {
		return Constants.SUCCESS.equals(this.code);
	}

	public ApiResult toApiResult() {
		return new ApiResult(this);
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
	}
}
